package com.lst.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.ClientOnlineCourseMapper;
import com.lst.entity.request.ClientCourseReqPara;
import com.lst.model.ClientOnlineCourse;
import com.lst.utils.RequestUtils;

public class DeleteMyOnlineClient extends BaseServlet {

    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private ClientOnlineCourseMapper clientOnlineCourseMapper = ctx
	    .getBean(ClientOnlineCourseMapper.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	BaseResponse resPara = new BaseResponse();
	try {
	    ClientCourseReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
		Integer clientid = Integer.valueOf(reqPara.getUserid());
		Integer courseid = Integer.valueOf(reqPara.getCourseid());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("clientid", clientid);
		map.put("courseid", courseid);

		List<ClientOnlineCourse> coclist = clientOnlineCourseMapper
			.queryList(map);
		if (coclist.isEmpty()) {
		    resPara.setCode(CommCode.M_A000000);
		    resPara.setMessage(CommCode.M_D000006);
		} else {

		    ClientOnlineCourse coc = coclist.get(0);

		    coc.setEnabled(false);
		    coc.setIsdeleted(true);
		    coc.setCreatedate(new Date());
		    coc.setUpdateuser(clientid);

		    clientOnlineCourseMapper.updateByPrimaryKeySelective(coc);

		    resPara.setCode(CommCode.M_Y000000);
		    resPara.setMessage(CommCode.M_Y000001);
		}
	    }
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);

	//    logger.info("DeleteMyOnlineClient jsonResult: " + jsonResult);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

	//    logger.info("doPost end run(s): "+ DateUtil.calLastedTime(startDate));
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public ClientCourseReqPara getReqPara(HttpServletRequest request) {

	ClientCourseReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new ClientCourseReqPara());

	    reqPara = (ClientCourseReqPara) baseRequest.clone();

	    String userid = request.getParameter("userid");
	    String courseid = request.getParameter("courseid");

	//    logger.info("reqPara userid: " + userid);

	    reqPara.setUserid(userid);
	    reqPara.setCourseid(courseid);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}
	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	ClientCourseReqPara ccReqPara = (ClientCourseReqPara) reqPara;

	if (StringUtils.isEmpty(ccReqPara.getUserid())) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_P000002);
	} else if (StringUtils.isEmpty(ccReqPara.getCourseid())) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_S000011);
	} else {
	    flag = true;
	}
	return flag;
    }

}
